//
//  CollectionButtonViewController.m
//  SYiDaiy
//
//  Created by dllo on 16/9/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CollectionButtonViewController.h"
#import "TAMacro.h"
#import "CustomTableViewCell.h"

#import "NewsESRootClass.h"
#import "Newsesarray.h"
#import "AFNetworking.h"
@interface CollectionButtonViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) UITableView *collecTableView;

@property (nonatomic,strong) NewsESRootClass *newsESRootClass;
@property (nonatomic,strong) NSMutableArray<Newsesarray *> *newsArray;
@end

@implementation CollectionButtonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createcollecTableView];
}

- (void)createcollecTableView
{
    _collecTableView = [[UITableView alloc] initWithFrame:SCREEN_RECT style:UITableViewStylePlain];
    _collecTableView.dataSource = self;
    _collecTableView.delegate = self;
    [self.view addSubview:_collecTableView];
    [_collecTableView registerClass:[CustomTableViewCell class] forCellReuseIdentifier:@"collecTableViewCell"];
    
    [self.collecTableView reloadData];
    [self newsNetworkGet];
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
//    NSLog(@"222222222222%ld",self.guid);
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"collecTableViewCell"];
    if (_newsArray[indexPath.row].guid == _guid) {
        cell.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
    cell.titleLabel.text = _newsArray[indexPath.row].title;
    cell.pubdateLabel.text = _newsArray[indexPath.row].pubdate;
    cell.summaryLabel.text = _newsArray[indexPath.row].summary;
    cell.cover_thumb_hdImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_newsArray[indexPath.row].cover_thumb_hd]]];
    } 
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _newsArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH * 140 / 375;
}









#pragma mark 网络请求
- (void)newsNetworkGet
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSString *requestURLString = @"http://watch-cdn.idailywatch.com/api/list/magazine/zh-hans?page=1&ver=iphone&app_ver=15";
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 60;
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager GET:requestURLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        _newsArray = [NSMutableArray array];
        for (NSDictionary *dic in responseObject) {
            Newsesarray *newsesarray = [Newsesarray modelWithDic:dic];
            [_newsArray addObject:newsesarray];
        }
        [self.collecTableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
    
    
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
